package com.dollkey.packagemanager;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;


import androidx.constraintlayout.widget.ConstraintLayout;

public class FilterEditWindow extends BasePopupWindow implements View.OnClickListener {

    private INameChangeListener mOnClick;
    private EditText mEt;
    private CheckBox mRadioButton;

    public FilterEditWindow(Context context, OnDismissListener onDismiss, INameChangeListener onClick) {
        super(context, onDismiss);
        mOnClick = onClick;
    }

    @Override
    protected View initView(LayoutInflater inflater) {
        ConstraintLayout inflate = (ConstraintLayout) inflater.inflate(R.layout.pop_filter_edit, null);
        inflate.setOnClickListener(this);
        inflate.findViewById(R.id.cancel_tv).setOnClickListener(this);
        inflate.findViewById(R.id.card_view).setOnClickListener(v -> {
        });
        inflate.findViewById(R.id.confirm_tv).setOnClickListener(this);
        inflate.findViewById(R.id.clear_tv).setOnClickListener(v -> mEt.setText(""));
        mEt = inflate.findViewById(R.id.edit_ed);
        mRadioButton = inflate.findViewById(R.id.radio_button);
        Constant.tintCursorDrawable(mEt, mContext.getResources().getColor(R.color.colorPrimary));
        mEt.setHint("输入应用名或包名关键过滤词，用回车或\",\"分隔");
        return inflate;
    }

    public FilterEditWindow setText(String text) {
        mEt.setText(text);
        return this;
    }

    public FilterEditWindow setFilter(boolean isFilter) {
        mRadioButton.setChecked(isFilter);
        return this;
    }

    public FilterEditWindow setMaxInput(final int maxInput) {
        mEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > maxInput) {
                    int c = count - (s.length() - maxInput);
                    s = s.subSequence(0, start + c).toString() + s.subSequence(start + count, s.length()).toString();
                    mEt.setText(s);
                    mEt.setSelection(start + c);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        return this;
    }

    @Override
    public void showAtCenter() {
        super.showAtCenter();
    }

    @Override
    public void onClick(View view) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(mEt.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
        switch (view.getId()) {
            case R.id.cancel_tv:
                break;
            case R.id.confirm_tv:
                mOnClick.confirm(mEt.getText().toString().trim(), mRadioButton.isChecked());
                break;
        }
        dismiss();
    }

    public interface INameChangeListener {
        void confirm(String name, boolean isFilter);
    }
}
